#!/usr/bin/perl

# split a large file to a sequence of files by line number
# For example: fileSplit.pl test.txt 200
# Mr.xuhan@gmail.com

if(@ARGV<2) {
	print "please input SOURCE file name and splitLine!\n";
	exit;
}

open(SOURCE, "<".@ARGV[0]) || die "Cannot read file";
$splitLine = @ARGV[1];

open(TEMPFH, "<".@ARGV[0]) || die "Cannot read file";
$linenum = 0;
while(<TEMPFH>) {
	$linenum = $.;
}
close(TEMPFH);

if($linenum%$splitLine == 0){
	$fileNum = int($linenum/$splitLine);
}
else {
	$fileNum = int($linenum/$splitLine)+1;
}

$k = 0;
@filenames = split(/\./,@ARGV[0]);

for($i = 1;$i<=$fileNum;$i++) {
	 open(OUT, ">".$filenames[0]."_".$i.".".$filenames[1]) || die "Cannot write file";		
		while(<SOURCE>) {
			$k++;
			print OUT $_;
			if($k>=($splitLine*$i)) {				
				last;
			}			
		}		
	 close(OUT);	
}






